package com.api.regression.group.editgroup;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.zyncro.api.IGroupApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.GroupApi;
import com.zyncro.api.model.ObjectResultApi;

@ContextConfiguration(locations = { "classpath:/group/group-context.xml" })
public class EditGroup extends AbstractApiTest {

	@Value("#{editgroups['user.groupOwner'].appId}")
	private String userUrn;

	@Value("#{editgroups['user.groupOwner'].password}")
	private String userPassword;

	@Value("#{editgroups['group.owned.urn']}")
	private String groupUrn;

	private String nameBefore;
	private String desciptionBefore;

	@BeforeClass
	protected void loginUser() throws Exception {

		getApi().login(userUrn, userPassword);

		ObjectResultApi<GroupApi> result = getApi().getService(IGroupApiService.class).getGroup(groupUrn);
		GroupApi group = result.getObject();

		this.nameBefore = group.getName();
		this.desciptionBefore = group.getDescription();
	}

	@Test
	public void testGetGroup() throws ZyncroApiException {

		String name = "Name after";
		String description = "Descripton after";
		String attributes = null;
		Boolean defaultVote = null;

		getApi().getService(IGroupApiService.class).editGroup(groupUrn, name, description, attributes, defaultVote);

		ObjectResultApi<GroupApi> result = getApi().getService(IGroupApiService.class).getGroup(groupUrn);
		GroupApi group = result.getObject();

		Assert.assertEquals(group.getName(), name, "Group name after edit");
		Assert.assertEquals(group.getDescription(), description, "Group name after edit");
	}

	@AfterClass
	public void restoreGroupValues() throws ZyncroApiException {

		Boolean defualtVote = null;

		getApi().getService(IGroupApiService.class).editGroup(groupUrn, nameBefore, desciptionBefore, null, defualtVote);
	}
}
